package com.itgaohe.controller;


import com.itgaohe.common.context.UserContext;
import com.itgaohe.dao.entity.User;
import com.itgaohe.dao.entity.UserInfo;
import com.itgaohe.dto.requ.LoginRequestDTO;
import com.itgaohe.dto.resp.SearchUserFriendsDTO;
import com.itgaohe.dto.resp.SearchUserInfo;
import com.itgaohe.service.UserService;
import com.itgaohe.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关操作")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     */
    @Operation(summary = "验证码", description = "返回所有用户数据")
    @GetMapping("/code")
    public Result sendCode(@RequestParam("phone") String phone) {
        return userService.sendCode(phone);
    }

    /**
     * 登录功能
     * @param requestParam 登录参数，包含手机号、验证码；或者手机号、密码
     */
    @Operation(summary = "用户登入")
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequestDTO requestParam){
        // 实现登录功能
        return userService.login(requestParam);
    }

    /**
     *
     * @param request
     * @return
     */
    @Operation(summary = "用户登出")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){
        // 实现登出功能
        return userService.logout(request);
    }

    /**
     * 根据用户的id多表联查查询用户对应的粉丝
     */
    @Operation(summary = "查询用户的粉丝")
    @GetMapping("/fans")
    public Result getAllFans(@RequestParam("userId") Long userId,
                             @RequestParam("cursor") Long cursor){
        return userService.findAllFansByUserId(userId,cursor);
    }

    /**
     *根据用户的id多表联查查询用户的关注
     */
    @Operation(summary = "查询用户的关注")
    @GetMapping("/follows")
    public Result getAllFollowers(@RequestParam("userId") Long userId,
                                  @RequestParam("cursor") Long cursor){
        return userService.findAllFollowersByUserId(userId,cursor);
    }

    /**
     *根据用户的id查询用户的详细信息
     */
    @Operation(summary = "查询用户的详细信息")
    @GetMapping("/info")
    public Result getUserInfo(){
        List<UserInfo> allUserInfoByUserId = userService.findAllUserInfoByUserId(UserContext.getUser().getId());
        return Result.ok(allUserInfoByUserId);
    }

    /*
    * 根据姓名姓名模糊查询用户信息
    * */
    @Operation(summary = "根据姓名模糊查询用户信息")
    @GetMapping("/getByName")
    public Result searchUsers(
            @RequestParam("nickName") String nickName,
            @RequestParam("cursor") Long cursor) {
        return userService.searchUsersByNickName(nickName, cursor);
    }

    /**
     * 查询用户好友
     */
    @Operation(summary = "根据用户 id 查询用户好友")
    @GetMapping("/friends")
    public Result getFriendsByUserId (
            @RequestParam("cursor") Long cursor) {
        return userService.searchUsersFriends(cursor);
    }

    /**
     * 查询用户详情
     */
    @Operation(summary = "查询用户详情")
    @GetMapping("/me")
    public Result getMe(){
        return Result.ok(userService.getById(UserContext.getUser().getId()));
    }

    /**
     * 用户关注
     * @param isFollow 0：表示去关注   1：表示去取关
     */
    @Operation(summary = "用户关注")
    @GetMapping("/tofollow/{userId}/{isFollow}")
    public Result toFollow (@PathVariable("userId") Long userId , @PathVariable("isFollow") Integer isFollow) {
        Result result = userService.toFollow(userId, isFollow);
        return result;
    }

    /**
     * 查询用户简单信息
     */
    @Operation(summary = "查询用户简单信息")
    @GetMapping("/infoMe")
    public Result getInfoMe(){
        return Result.ok(UserContext.getUser());
    }

}
